package com.heatingcharge.controller.unit;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heatingcharge.common.util.R;
import com.heatingcharge.dto.ResBuildingDTO;
import com.heatingcharge.dto.ResZoneDTO;
import com.heatingcharge.dto.ZoneClearOffDTO;
import com.heatingcharge.entity.unit.ResBuilding;
import com.heatingcharge.entity.unit.ResZone;
import com.heatingcharge.param.ResZoneParam;
import com.heatingcharge.param.SearchParam;
import com.heatingcharge.param.ZoneTreeParam;
import com.heatingcharge.service.charge.IClearOffService;
import com.heatingcharge.service.unit.IResBuildingService;
import com.heatingcharge.service.unit.IResZoneService;
import com.heatingcharge.vo.TreeVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 小区  前端控制器
 * </p>
 *
 * @author devin.jiang
 * @since 2019-07-12
 */
@RestController
@RequestMapping("/res-zone")
public class ResZoneController {

    @Autowired
    IResZoneService resZoneService;
    @Autowired
    IResBuildingService buildingService;
    @Autowired
    IClearOffService clearOffService;

    @ApiOperation(value = "分页获取小区信息",notes = "分页获取小区信息")
    @GetMapping
    public R getZone(Page page, ResZoneDTO resZoneDTO){
        IPage<ResZone> zoneIPage = resZoneService.selectZonePage(page, resZoneDTO);
        return R.ok(zoneIPage);
    }

    @ApiOperation(value = "获取全部小区信息",notes = "获取全部小区信息")
    @GetMapping("/all")
    public R getAllZone(ResZoneDTO resZoneDTO){
        List<ResZone> zones = resZoneService.selectZone(resZoneDTO);
        return R.ok(zones);
    }

    @ApiOperation(value = "获取小区树信息",notes = "获取全部小区信息")
    @GetMapping("/tree")
    public R getAllZoneTree(ZoneTreeParam zoneTreeParam){
        List<TreeVo> zones = resZoneService.selectZoneTree(zoneTreeParam);
        TreeVo treeVo=TreeVo.builder()
                .id("0")
                .label("全部")
                .type("all")
                .children(zones)
                .build();
        if (zoneTreeParam.getShowStatistics().equals(true)){
            SearchParam searchParam=new SearchParam(0,0,zoneTreeParam.getHeatYearId());
            ZoneClearOffDTO allZonesStatistics = clearOffService.getAllZonesStatistics(searchParam);
            treeVo.setZoneClearOffDTO(allZonesStatistics);
        }
        return R.ok(treeVo);
    }

    @ApiOperation(value = "根据小区ID获取楼栋信息",notes = "根据小区ID获取楼栋信息")
    @GetMapping("/{zoneId}/res-building")
    public R getBuildingByZoneId(ResBuildingDTO resBuildingDTO){
        List<ResBuilding> resBuildings = buildingService.selectBuildingByZoneId(resBuildingDTO);
        return R.ok(resBuildings);
    }
    @ApiOperation(value = "新增小区",notes = "新增小区")
    @PostMapping
    public R saveZone(@RequestBody ResZoneParam resZoneParam){
        if (resZoneService.save(resZoneParam)){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "更新小区",notes = "更新小区")
    @PutMapping
    public R updateZone(@RequestBody ResZoneParam resZoneParam){
        if (resZoneService.update(resZoneParam)){
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "删除小区",notes = "根据主键ID删除小区")
    @DeleteMapping("/{id}")
    public R delZone(@PathVariable("id") Integer id){
        if (resZoneService.removeById(id)){
            return R.ok();
        }
        return R.error();
    }
}

